package com.agd.quick.states;

import java.util.Random;

import lib.elementgame.GameSprite;
import lib.elementgame.GameText;
import lib.engine.Anchor;
import lib.engine.GameEngine;
import lib.engine.GameState;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

public class StateTips extends GameState {
	
	private static GameSprite tips[] = new GameSprite[4];
	
	Random rand = new Random();
//	private int tips_count;
	//make siklus 
	private int tips_count = 0;
	private GameText tap;
	private int hitung = 0;

	public StateTips(GameEngine engine) {
		super(engine);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		tips_count++;
		if(tips_count == 3){
			tips_count = 0;
		}
		exitState(GamePlayState);
		return false;
	}

	@Override
	public void initComponent() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 4; i++){
			tips[i] = new GameSprite(TIPS_1 + i, engine);
		}
		tap = new GameText("TAP TO CONTINUE",50, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
	}

	@Override
	public void initLoading() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
//		tips_count = rand.nextInt(4);
		
		tips[tips_count].setVisible(true);
		for(int i = 0; i < 4; i++){
			if(i != tips_count){
				tips[i].setVisible(false);
			}
		}
		tips[0].setWidth(800);
		tips[0].setHeight(480);
		tips[1].setWidth(800);
		tips[1].setHeight(480);
		tips[2].setWidth(800);
		tips[2].setHeight(480);
		tips[3].setWidth(800);
		tips[3].setHeight(480);
		hitung = 0;
	}

	@Override
	protected void attach() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 4; i++){
			engine.scene.attachChild(tips[i]);
		}
		engine.scene.attachChild(tap);
	}

	@Override
	protected void detach() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 4; i++){
			tips[i].detachSelf();
		}
		tap.detachSelf();
	}

	@Override
	protected void setPosition() {
		// TODO Auto-generated method stub
		tap.setPosition(Anchor.BOTTOM_CENTER);
	}

	@Override
	protected void registerTouch() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 4; i++){
			engine.scene.registerTouchArea(tips[i]);
		}
	}

	@Override
	protected void unregisterTouch() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 4; i++){
			engine.unregisterSceneTouch(tips[i]);
		}
	}

	@Override
	protected void onUpdate() {
		// TODO Auto-generated method stub
		hitung++;
		if(hitung%16 == 8){
			tap.setAlpha(0.5f);
		}
		if(hitung%16 == 0){
			tap.setAlpha(1);
			hitung = 0;
		}
		
	}

	@Override
	protected void onPaused() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onResumed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
	}

}
